import settings
from lib import get_server_info
from concurrent.futures import ThreadPoolExecutor


def ssh(hostname, cmd):
    import paramiko
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    # 允许连接不在know_hosts文件中的主机
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
    # 连接服务器
    ssh.connect(hostname=hostname, port=settings.SSH_PORT, username=settings.SSH_USER, password=settings.SSH_PWD)
    # 执行命令
    stdin, stdout, stderr = ssh.exec_command(cmd)
    # 获取命令结果
    result = stdout.read()
    # 关闭连接
    ssh.close()
    info = result.decode('utf-8')
    return info


def task(host):
    server_info = get_server_info(ssh, host)
    server_info['hostname'] = host
    print(server_info)
    """
    然后这里把获取到的结果存入到数据库或者发送给api平台进行存储
    """


def run():
    pool = ThreadPoolExecutor(10)  # 为了提高性能通过线程池的方式
    host_list = ['192.168.1.99']
    for host in host_list:
        pool.submit(task, host)


if __name__ == '__main__':
    run()
